package com.tanhua.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisByDayMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.vo.DashboardSummaryVo;
import com.tanhua.service.UserApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * @author げっしょく
 * @date 2021/11/9
 */
@Service
public class DashboardService {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Autowired
    private LogMapper logMapper;

    @DubboReference
    private UserApi userApi;

    public DashboardSummaryVo summary() {
        //查询累计用户
        Integer cumulativeUsers = userApi.getCountUser();
        //过去30天活跃用户
        LocalDate time = LocalDate.now();
        LocalDate months = time.plus(-1, ChronoUnit.MONTHS);
        Integer monthsCount = analysisByDayMapper.getMonths(time, months);
        //过去7天活跃用户
        LocalDate week = time.plus(-1, ChronoUnit.WEEKS);
        Integer weekCount = analysisByDayMapper.getMonths(time, week);
        // 今日新增用户
        Integer enrollment = logMapper.getEnrollment(time.toString());
        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        LocalDate yesterday = time.plus(-1, ChronoUnit.DAYS);
        Integer yesterdayEnrollment = logMapper.getEnrollment(yesterday.toString());
        int riseEnrollment = (enrollment - yesterdayEnrollment) - yesterdayEnrollment * 100;
        //今日登录次数
        Integer login = logMapper.getloginCount(time.toString());
        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Integer yesterdayLogin = logMapper.getloginCount(yesterday.toString());
        int riseLogin = (login - yesterdayLogin) - yesterdayLogin * 100;
        //今日活跃用户
        Integer active = logMapper.getActiveCount(time.toString());
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer yesterdayActive = logMapper.getActiveCount(yesterday.toString());
        int riseActive = (active - yesterdayActive) - yesterdayActive * 100;
        DashboardSummaryVo dashboardSummaryVo = new DashboardSummaryVo();
        dashboardSummaryVo.setCumulativeUsers(cumulativeUsers);
        dashboardSummaryVo.setActivePassMonth(monthsCount);
        dashboardSummaryVo.setActivePassWeek(weekCount);
        dashboardSummaryVo.setNewUsersToday(enrollment);
        dashboardSummaryVo.setNewUsersTodayRate(riseEnrollment);
        dashboardSummaryVo.setLoginTimesToday(login);
        dashboardSummaryVo.setLoginTimesTodayRate(riseLogin);
        dashboardSummaryVo.setActiveUsersToday(active);
        dashboardSummaryVo.setActiveUsersTodayRate(riseActive);
        return dashboardSummaryVo;

    }

/*    public static void main(String[] args) {


        LocalDate today = LocalDate.now();
        System.out.println("今天的日期为:" + today.toString());
        LocalDate plus = today.plus(-1, ChronoUnit.DAYS);
        System.out.println("昨天的日期为:" + plus);
        LocalDate nextWeek = today.plus(-1, ChronoUnit.WEEKS);
        System.out.println("一周后的日期为:" + nextWeek);


    }*/
}
